const router = require('koa-router')()
const path = require('path')
const fs = require('fs')

const coursePath = path.join(path.resolve(__dirname, '../../'), '/public/images/course_imgs')
const avatarPath = path.join(path.resolve(__dirname, '../../'), '/public/images/avatars')

// 生成雪花ID的方法
const createSnow = function (ctx) {
    let snowFlake = ctx.snowFlake;
    let snowId = snowFlake.createSnow(5);
    return snowId;
}

router.prefix('/image')


// 返回具体图片内容
router.get('/getcourseimage/:name', function (ctx, next) {
    // 动态路由的参数
    console.log('name:', ctx.params.name);
    let filePath = path.join(coursePath, decodeURIComponent(ctx.params.name));
    let fileContent = fs.readFileSync(filePath);
    ctx.body = fileContent
})
// 返回具体图片内容
router.get('/getavatar/:name', function (ctx, next) {
    // 动态路由的参数
    console.log('name:', ctx.params.name);
    let filePath = path.join(avatarPath, decodeURIComponent(ctx.params.name));
    let fileContent = fs.readFileSync(filePath);
    ctx.body = fileContent
})

module.exports = router